﻿using Mobileterminal.Common;
using Mobileterminal.Dtos;
using Newtonsoft.Json;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mobileterminal.Forms
{
    public partial class AddQueryTypeForm : UIEditForm
    {
        public AddQueryTypeForm()
        {
            InitializeComponent();
            // 配置图片框显示模式
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            pictureBox1.AllowDrop = true;

            // 添加拖放事件
            /*   pictureBox1.DragEnter += PictureBox1_DragEnter;
               pictureBox1.DragDrop += PictureBox1_DragDrop;*/
            GetAppsType();  //下拉框显示




        }

        private async void GetAppsType()
        {
            var url = "https://localhost:7224/api/ApparatusType/QueryApparatusType";


            var json = await HttpClientHelper.ClientAsync("GET", url);
            var result = Newtonsoft.Json.JsonConvert.DeserializeObject<Result<List<AppsTypes>>>(json);
            if (result.Code == 200)
            {
                uiComboBox1.DataSource = result.Data;
                uiComboBox1.DisplayMember = "ApparatusTypeName";
                uiComboBox1.ValueMember = "ApparatusTypeId";
                appsTypes = result.Data;
            }

        }

        // 在类中添加字段存储上传的图片路径
        private string uploadedImagePath = string.Empty;
/*        /// <summary>
        /// 
        /// 处理拖放文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void PictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            try
            {
                var files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files != null && files.Length > 0)
                {
                    var ext = Path.GetExtension(files[0]).ToLower();
                    if (new[] { ".jpg", ".jpeg", ".png", ".bmp" }.Contains(ext))
                    {
                        uploadedImagePath = files[0];
                        pictureBox1.Image?.Dispose();
                        pictureBox1.Image = Image.FromFile(uploadedImagePath);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件加载失败：" + ex.Message);
            }
        }
        /// <summary>
        /// 拖放文件进入时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void PictureBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }*/

        private void AddQueryTypeForm_Load(object sender, EventArgs e)
        {

        }

        private void uiLabel4_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp";
                openFileDialog.Title = "选择分类图片";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    uploadedImagePath = openFileDialog.FileName;
                    pictureBox1.Image?.Dispose(); // 释放旧图片资源
                    pictureBox1.Image = Image.FromFile(uploadedImagePath);
                }
                MessageBox.Show("图片路径：" + uploadedImagePath);
            }
        }

        private void uiLabel1_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel2_Click(object sender, EventArgs e)
        {

        }

        private void uiLabel3_Click(object sender, EventArgs e)
        {

        }

        private void uiTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void uiTextBox2_TextChanged(object sender, EventArgs e)
        {

        }
        public static List<AppsTypes> appsTypes = new List<AppsTypes>();
        private async void btnOK_Click(object sender, EventArgs e)
        {
            var type = appsTypes.FirstOrDefault(x => x.ApparatusTypeName == uiComboBox1.Text);
            //传参
            var pdata = new
            {
                ApparatusTypeId = type.ApparatusTypeId,
                ApparatusTypeName = uiTextBox1.Text,
                ApparatusImg=uploadedImagePath,
                Aid= uiTextBox2.Text,


            };

            string jsonParam = JsonConvert.SerializeObject(pdata);
            HttpContent httpContent = new StringContent(jsonParam, Encoding.UTF8, "application/json");
            var json = await HttpClientHelper.ClientAsync("post", "https://localhost:7286/api/Apparatus/CreateApparatusType", true, httpContent);
            CreateApparatusTypeDtos result = JsonConvert.DeserializeObject<CreateApparatusTypeDtos>(json);

            if (result != null)
            {
                UIMessageTip.Show("添加成功");

                this.DialogResult = DialogResult.OK;  // 先设置结果
                this.Close();
                return;
            }
        }

        private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
